package controller;

import dao.UserDao;
import model.JDBCUtil;
import model.User;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@WebServlet("/login/*")
public class loginServlet extends BaseServlet {
    public int checkUser(HttpServletRequest request,HttpServletResponse response)  {
        String username = request.getParameter("form-username");
        String password = request.getParameter("form-password");
        String checkCode = request.getParameter("checkCode");
        Object code = request.getSession().getAttribute("sessionCode");
        if (!checkCode.equals(code)){
            return 3;  //验证码错误
        }
        JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtil.getDataSource());
        List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from user");
        try {
        PrintWriter pw = response.getWriter();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).get("username").equals(username)){
                if (list.get(i).get("password").equals(password)){
                    pw.write("登录成功");
                    User user = new UserDao().findByUsername(username);
                    request.getSession().setAttribute("user",user);
                        response.sendRedirect(request.getContextPath()+"/html/home.jsp");
                    return 1;   //登录成功
                }
                pw.write("密码错误");
                return 2;   //密码错误
            }
        }
            pw.write("没有该用户");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;   //没有该用户
    }

    public int register(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
        String Email = request.getParameter("form-email");
        String username = request.getParameter("form-username");
        String password1 = request.getParameter("form-password");
        String password2 = request.getParameter("form-password");
        request.setAttribute("Email",Email);
        request.setAttribute("username",username);
        request.setAttribute("password1",password1);
        request.setAttribute("password2",password2);
        JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtil.getDataSource());
        List<Map<String, Object>> list = jdbcTemplate.queryForList("SELECT username FROM user ");
        for (Map<String, Object> map : list) {
            if (map.get("username").equals(username)) {
                PrintWriter pw = response.getWriter();
                pw.write("注册失败");
                pw.close();
                return 0;
            }
        }
        if (!password1.equals(password2)){
            PrintWriter pw = response.getWriter();
            pw.write("密码不一致");
            pw.close();
            return 1;
        }
        jdbcTemplate.update("insert into user (email,username,password) values (?,?,?)",Email,username,password1);
        response.sendRedirect(request.getContextPath()+"/index.jsp");
        return 2;
    }

    public int registerName(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        PrintWriter pw = response.getWriter();
        JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtil.getDataSource());
        List<Map<String, Object>> list = jdbcTemplate.queryForList("SELECT username FROM user ");
        for (Map<String, Object> map : list) {
            if (map.get("username").equals(username)){
                pw.write("用户名已存在");
                pw.close();
                return 0;
            }
        }
        pw.write("用户名可以使用");
        pw.close();
        return 1;
    }

    public int registerPwd(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String password1 = request.getParameter("password1");
        String password2 = request.getParameter("password2");
        PrintWriter pw = response.getWriter();
        if (password1.equals(password2)){
            pw.write("密码可以使用");
            pw.close();
            return 1;
        }else {
            pw.write("密码不一致");
            pw.close();
            return 0;
        }
    }

    public int registerEmail(HttpServletRequest request,HttpServletResponse response) throws IOException{
        String email = request.getParameter("email");
        PrintWriter pw = response.getWriter();
        JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtil.getDataSource());
        List<Map<String, Object>> list = jdbcTemplate.queryForList("SELECT email FROM user");
        for (Map<String, Object> map : list) {
            if (map.get("email")==null){
                continue;
            }
            if (map.get("email").equals(email)){
                pw.write("邮箱已注册");
                pw.close();
                return 0;
            }
        }
        pw.write("邮箱可以使用");
        pw.close();
        return 1;
    }
}
